/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package race;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.media.MediaException;
import race.component.RaceCanvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDlet;
import race.component.SettingForm;
import race.util.BackgroundUtil;
import race.util.Tool;

/**
 *
 * @author BBXP
 */
public class MotorBikeGameMIDlet extends MIDlet implements CommandListener{
    private Display display;
    private SettingForm settingForm;
    private Form mainForm;
    private RaceCanvas rc;
    private Command startC;
    private Command settingC;
    private Command exitC;
    private Command backC;
    private Command restartC;
    private Command pause;
    private Command resumeC;
    private Command instructionC;
    private Thread t;
    private Form instructionForm;
    private Player menuPlayer;
    private Player gamePlayer;

    protected void destroyApp(boolean unconditional) {
        //TODO gc.stop();
        Tool.closeRecord();
    }

    protected void pauseApp() {
        //rc.pause();
        stopPlayers();
    }



    protected void startApp(){
       inti();
       addAndsetCommand();
       display.setCurrent(mainForm);
//       int[] a = BackgroundUtil.getCollisionMap(1);
//       for(int i = 0; i < a.length; i++){
//           if(i % 11 == 0 && i != 0){
//               System.out.println("");
//           }
//           System.out.print(a[i]+" ");
//       }
    }

    private void inti(){
        Tool.openMyRecord();
        initSound();
        instructionForm = new Form("Instruction");
        instructionForm.append(Tool.getInstruction());
        display = Display.getDisplay(this);
        rc = new RaceCanvas();
        settingForm = new SettingForm();
        mainForm = new Form("Race Game");
        try {
            mainForm.append(Image.createImage("/images/main_resized.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        exitC = new Command("exit", Command.EXIT, 0);
        settingC = new Command("option", Command.ITEM, 1);
        backC = new Command("return", Command.BACK, 0);
        restartC = new Command("restart", Command.ITEM, 1);
        startC = new Command("start", Command.ITEM, 1);
        pause = new Command("pause", Command.ITEM, 1);
        resumeC = new Command("resume", Command.ITEM, 1);
        instructionC = new Command("Instruction", Command.ITEM, 1);
        t =  new Thread(rc);
    }

    private void initSound(){
        InputStream is = getClass().getResourceAsStream("/sounds/menuSound.mid");
        try {
            menuPlayer = Manager.createPlayer(is, "audio/midi");
            menuPlayer.prefetch();
            menuPlayer.setLoopCount(-1);
            menuPlayer.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        is = getClass().getResourceAsStream("/sounds/gameSound.mid");
        try {
            gamePlayer = Manager.createPlayer(is, "audio/midi");
            gamePlayer.prefetch();
            gamePlayer.setLoopCount(-1);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }

    private void addAndsetCommand(){
        mainForm.setCommandListener(this);
        rc.setCommandListener(this);
        settingForm.setCommandListener(this);
        instructionForm.setCommandListener(this);
        mainForm.addCommand(exitC);
        mainForm.addCommand(startC);
        mainForm.addCommand(settingC);
        mainForm.addCommand(instructionC);
        settingForm.addCommand(backC);
        instructionForm.addCommand(backC);
        rc.addCommand(backC);
        rc.addCommand(resumeC);
        rc.addCommand(restartC);
        rc.addCommand(settingC);
    }
    public void commandAction(Command c, Displayable d) {
        if(d == rc){
            
            rc.pause();
        }
        if(c == settingC){
            continuePlayMenuSound();
            settingForm.refreshRecords();
            display.setCurrent(settingForm);
        }else if(c == startC){
            continuePlayGameSound();
            rc.setLevel(settingForm.getLevel());
            rc.reset();
            if(rc.isStopped()){
                t = new Thread(rc);
                t.start();
            }
            display.setCurrent(rc);
        }else if(c == restartC){
            resetGameSound();
            continuePlayGameSound();
            rc.setLevel(settingForm.getLevel());
            rc.reset();
            rc.resume();
            try {
                gamePlayer.start();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
        }else if(c == backC){
            if(!rc.isStopped() && d == settingForm){
                continuePlayGameSound();
                display.setCurrent(rc);
            }else{
                continuePlayMenuSound();
                display.setCurrent(mainForm);
            }
        }else if(c == exitC){
            this.notifyDestroyed();
            this.destroyApp(true);
        }else if(c == resumeC){
            continuePlayGameSound();
            rc.resume();
        }else if(c == instructionC){
            continuePlayMenuSound();
            display.setCurrent(instructionForm);
        }
    }

    private void resetMenuSound(){
        try {
            menuPlayer.setMediaTime(-1);
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }
    private void resetGameSound(){
        try {
            gamePlayer.setMediaTime(-1);
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }
    private void continuePlayGameSound(){
        try {
            menuPlayer.stop();
            gamePlayer.start();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }
    private void continuePlayMenuSound(){
        try {
            gamePlayer.stop();
            menuPlayer.start();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }
    private void closePlayer(){
        gamePlayer.close();
        menuPlayer.close();
    }
    private void stopPlayers(){
        try {
            gamePlayer.stop();
            menuPlayer.stop();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }

    }
    
}
